﻿using System;
using System.Web;

using Slja.BigFileUpload;//大文件引用命名空间
using Newtonsoft.Json;//对象到JSON的相互转换
using System.Text.RegularExpressions;
using Slja.Lib;//正则
namespace Slja.WEB.share
{
    public class progressbar : IHttpHandler
    {

        private string template = "{{statue:'{0}',msg:{1}}}";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                string guid = context.Request["UploadID"];
                string cancel = context.Request["cancel"];
                UploadContext c = UploadContextFactory.GetUploadContext(guid);
                if (!string.IsNullOrEmpty(cancel))
                {
                    c.Abort = true;

                    throw new Exception("用户取消");
                }
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(c);
                if (string.IsNullOrEmpty(json) || json == "null")
                {
                    //WriteResultJson(1, c, context, template);
                }
                else
                {

                    WriteResultJson(1, json, context, template);
                }

            }
            catch (Exception err)
            {
                WriteResultJson(0, err.Message, context);
            }
        }



        public static void WriteResultJson(int resultno, string description, HttpContext context)
        {
            WriteResultJson(resultno, description, context, "{{statue:'{0}',msg:'{1}'}}");
        }

        public static void WriteResultJson(int resultno, string description, HttpContext context, string template)
        {
            description = StringHandle.ClearBR(StringHandle.ReplaceString(description, "'", "|", false));
            context.Response.Write(string.Format(template, resultno, description));
        }      

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
       

